package com.singzi.leetcode.N100;

import java.util.HashMap;

public class T0013RomanToNumber {
    public static void main(String[] args) {

        int a;
        Solution0013 solution0013 = new Solution0013();
        String s = "III";
        a = solution0013.romanToInt(s);
        System.out.println(a);
    }

}

class Solution0013{
    public int romanToInt(String s) {
        int sum = 0;
        HashMap<Object, Integer> roman = new HashMap<>();
        getHash(roman);
        System.out.println(s);
//        System.out.println(roman.get("IV"));
        for(int i = 0;i < s.length();i++){
            String str = String.valueOf(s.charAt(i));

//            char temp1 = s.charAt(i);
//            char temp2 = s.charAt(i+1)
            if(i < s.length()-1){
                if(s.charAt(i) == 'I'&&(s.charAt(i+1) == 'V'||s.charAt(i+1) == 'X')||
                        s.charAt(i) == 'X'&&(s.charAt(i+1) == 'L'||s.charAt(i+1) == 'C')||
                        s.charAt(i) == 'C'&&(s.charAt(i+1) == 'D'||s.charAt(i+1) == 'M')
                ){
                    str = str + String.valueOf(s.charAt(i+1));
                    i++;
                }
            }
            sum = sum+roman.get(str);
//            System.out.println(roman.get(str));
//            System.out.println(s.charAt(i));
        }
        return sum;
    }
    public HashMap<Object, Integer> getHash(HashMap<Object, Integer>hashMap){

        hashMap.put("I",1);
        hashMap.put("V",5);
        hashMap.put("X",10);
        hashMap.put("L",50);
        hashMap.put("C",100);
        hashMap.put("D",500);
        hashMap.put("M",1000);
        hashMap.put("IV",4);
        hashMap.put("IX",9);
        hashMap.put("XL",40);
        hashMap.put("XC",90);
        hashMap.put("CD",400);
        hashMap.put("CM",900);
        return hashMap;
    }
}
